package webser

import (
	"fmt"
	"log"
	"logs"
	"math"
	"net/http"
	"strconv"
	"strings"
	"tcpNetWorking/lib"
)

func getDataPages(getType string, getLan string, knumber_s string, page_s string) string {
	knumber, _ := strconv.Atoi(knumber_s)
	page, _ := strconv.Atoi(page_s)
	var filterDatas []lib.ComplexData
	for i, k := range lib.SaveDatasMap {
		if getLan == i {
			filterDatas = k
		}
	}

	if knumber > len(filterDatas) {
		knumber = len(filterDatas)
	}
	allpage := int(math.Ceil(float64(len(filterDatas)) / float64(knumber)))
	if page > allpage {
		page = allpage
	}
	if page == 1 {
		return getDataType(getType, filterDatas[:knumber*page], allpage)
	} else if page == allpage {
		return getDataType(getType, filterDatas[(page-1)*knumber:], allpage)
	} else {
		return getDataType(getType, filterDatas[(page-1)*knumber:knumber*page], allpage)
	}
}
func getDataType(inputType string, showDatas []lib.ComplexData, totalPages int) string {
	var redata string
	switch inputType {
	case "/host":
		var array []string
		for _, k := range showDatas {
			var re string
			re = "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.Host) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/mem":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.Memory) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/load":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.CpuLoad) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/diskUsed":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.DiskUsed) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/process":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.Process) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/diskInfo":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.DiskInfo) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/flow":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.Flow) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/network":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.NetWorkInfo) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/cpuInfo":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.CpuInfo) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	case "/cpuUsed":
		var array []string
		for _, k := range showDatas {
			re := "{\"dataTime\":\"" + k.NowTime + "\",\"Lan\":\"" + k.Lan + "\",\"Ip\":\"" + k.Ip + "\",\"Data\":" + fmt.Sprint(k.CpuUsed) + "}"
			array = append(array, re)
		}
		redata = "{\"TotalPages\":" + fmt.Sprint(totalPages) + ",\"redatas\":[" + strings.Replace(strings.Trim(fmt.Sprint(array), "[]"), " ", ",", -1) + "]}"
	default:
		redata = "[]"
	}
	return redata
}
func GetApi(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")             //允许访问所有域
	w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型
	w.Header().Set("content-type", "application/json")             //返回数据格式是json

	r.ParseForm() //解析参数，默认是不会解析的
	re := "Api is running!"
	if len(r.Form) != 0 {
		Lan := "Default"
		getLan, _getLan := r.Form["lan"]
		if _getLan {
			Lan = getLan[0]
		}
		pageSize, _pageSize := r.Form["pageSize"]
		if _pageSize {
			logs.PrintlnFile("[webser] Get "+r.URL.Path+" => pageSize : "+strings.Join(pageSize, ""), 3)
		} else {
			pageSize = append(pageSize, "24")
			logs.PrintlnFile("[webser] Get "+r.URL.Path+" => pageSize : 24", 3)
		}
		page, _page := r.Form["page"]
		if _page {
			logs.PrintlnFile("[webser] Get "+r.URL.Path+"=> page : "+strings.Join(page, ""), 3)
		} else {
			page = append(page, "1")
			logs.PrintlnFile("[webser] Get "+r.URL.Path+"=> page : 1", 3)
		}
		re = getDataPages(r.URL.Path, Lan, pageSize[0], page[0])
	}
	fmt.Fprintf(w, re) //这个写入到w的是输出到客户端的
}
func HelloWrold(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Fprintf(w, "Hello Wrold!")
}
func Start() {
	http.HandleFunc("/", HelloWrold)
	http.HandleFunc("/host", GetApi) //设置访问的路由
	http.HandleFunc("/mem", GetApi)
	http.HandleFunc("/load", GetApi)
	http.HandleFunc("/diskUsed", GetApi)
	http.HandleFunc("/process", GetApi)
	http.HandleFunc("/diskInfo", GetApi)
	http.HandleFunc("/flow", GetApi)
	http.HandleFunc("/network", GetApi)
	http.HandleFunc("/cpuInfo", GetApi)
	http.HandleFunc("/cpuUsed", GetApi)
	err := http.ListenAndServe(lib.Api, nil) //设置监听的端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
